#! /usr/bin/env python

#
# Date://02/11/2011 05:40:01 PM (CST) 
# Author: Gou Pengfei
#

import sys
import os
import optparse
import datetime
import re
from subprocess import call
from os.path import join as joinpath

progname = os.path.basename(sys.argv[0])

Usage = '''usage: %prog [options] will generate configuration
files automatically with accordance to the options given by
the user

Enjoy!
'''

optparser = optparse.OptionParser(Usage)

optparser.add_option('-n', '--job_num', dest='job_number',
                     default='1',
                     help='Number of jobs to kill'
                     " (default: '%default')" )
optparser.add_option('-s', '--start_id', dest='start_id',
                     default='14000',
                     help='Start job id of this killing'
                     " (default: '%default')")
                    
(options,leftovers) = optparser.parse_args()

# Call os.system() and raise exception if return status is non-zero
def system(cmd):
    try:
        retcode = call(cmd, shell=True)
        if retcode < 0:
            print >>sys.stderr, "Child was terminated by signal", -retcode
            print >>sys.stderr, "When attemping to execute: %s" % cmd
        elif retcode > 0:
            print >>sys.stderr, "Child returned", retcode
            print >>sys.stderr, "When attemping to execute: %s" % cmd
    except OSError, e:
        print >>sys.stderr, "Execution failed:", e
        print >>sys.stderr, "When attemping to execute: %s" % cmd

if __name__ == '__main__':

    start_id = int(options.start_id)
    num_to_kill = int(options.job_number)

    for n in range(num_to_kill):
        cmd = 'qdel %d' % (start_id + n)
        print cmd
        system(cmd)

    sys.exit(0)

